// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUi from 'element-ui'
// import { Button, Select, Menu, MenuItem } from 'element-ui'

import NProgress from 'vue-nprogress'
import App from './App'
import router from './router'
import store from './store'

import { TOGGLE_SIDEBAR } from './store/mutation-types'

import 'element-ui/lib/theme-default/index.css'
import './css/common.css'

Vue.use(ElementUi)	// 注册UI框架

Vue.use(NProgress)

// Vue.use(Button)
// Vue.use(Select)
// Vue.use(Menu)
// Vue.use(MenuItem)

Vue.config.productionTip = false

// 务必在加载 Vue 之后，立即同步设置以下内容
Vue.config.devtools = true

// sync(store, router)

const nprogress = new NProgress({ parent: '.nprogress-container' })

const { state } = store

// 利用vue-router提供的钩子函数beforeEach()对路由进行判断
router.beforeEach((to, from, next) => {
  if (state.app.device.isMobile && state.app.sidebar.opened) {
    store.commit(TOGGLE_SIDEBAR, false)
  }
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
        if (store.state.token) {  // 通过vuex state获取当前的token是否存在
            next();
        }
        else {
            next({
                path: '/login',
                query: {redirect: to.fullPath}  // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    }
    else {
        next();
    }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  nprogress,
  template: '<App/>',
  components: { App }
})
